package com.siruier.boss.ui.activity.user

import android.view.View
import androidx.core.view.isVisible
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.GiftOrderDetailBean
import com.siruier.boss.databinding.ActivityPhonecardOrderBinding
import com.siruier.boss.databinding.ItemPhonecardOrderBinding
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.utils.GlideUtils

class PhonecardOrderActivity : BaseActivity<ActivityPhonecardOrderBinding>() {

    private lateinit var mRefreshHelper: RefreshHelper<GiftOrderDetailBean>
    private val mPhonecardOrderAdapter = PhonecardOrderAdapter()

    override fun initView() {
        mPhonecardOrderAdapter.setOnClickAdapterListenter(::onClick)
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvOrder, mPhonecardOrderAdapter, listener = ::onReload)
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({
            mallApi.mobilePackagePage(mRefreshHelper.page).toData()?.records
        }, mRefreshHelper.handApiObserver())
    }

    private fun onClick(view: View, position: Int) {
        when (view.id) {
            R.id.button_supplementary_information -> {
                startActivityExt<SuppleInfoActivity> {
                    putExtra("orderSn", mPhonecardOrderAdapter.getItem(position).orderSn)
                }
            }
        }
    }
}

class PhonecardOrderAdapter : BaseRecyclerAdapter<ItemPhonecardOrderBinding, GiftOrderDetailBean>() {

    override fun onBindViewHolder(vb: ItemPhonecardOrderBinding, bean: GiftOrderDetailBean, position: Int) {
        // 目前只能购买一个商品
        bean.goodsList.takeIf { it.size > 0 }?.get(0)?.apply {
            GlideUtils.intoView(vb.ivImage.context, picUrl, vb.ivImage)
            vb.tvNumber.text = "x${number}"
            vb.tvGoodsName.text = goodsName
            vb.tvGoodsMoney.text = priceDesc
        }
        //审核状态 0-未提交 1-待审核 2-审核通过 3-审核拒绝
        vb.tvState.text = when (bean.auditStatus) {
            1 -> "待审核"
            2 -> "审核通过"
            3 -> "审核拒绝"
            else -> "未提交"
        }
        vb.tvRejectReason.isVisible = bean.rejectReason.isNotEmpty()
        vb.tvRejectReason.text = "拒绝原因：" + bean.rejectReason
        vb.buttonSupplementaryInformation.isVisible = bean.auditStatus != 2
        setViewClick(position, vb.buttonSupplementaryInformation)
    }
}